#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main()
{
	int n, m; int arr[10][10]; int sum = 0;
	while (scanf("%d%d",&m,&n) == 2)
	{
		int i, j;
		for (i = 0; i < m; i++)
		{
			for (j = 0; j < n; j++)
			{
				scanf("%d", &arr[i][j]);
			}
		}
		for (j = 0; j < n; j++)
		{
			int tmp = 0;
			tmp = arr[m- 1][j];
			arr[m- 1][j] = arr[0][j];
			arr[0][j] = tmp;
		}
		for (i = 0; i < m; i++)
		{
			for (j = 0; j < n; j++)
			{
				printf("%d ", arr[i][j]); sum += arr[i][j];
			}
			printf("%d\n", sum); sum = 0;
		}
	}

	return 0;
}